package cn.iocoder.yudao.module.team.service.line;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.team.controller.admin.line.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.line.LineDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.team.dal.mysql.line.LineMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.team.enums.ErrorCodeConstants.*;

/**
 * 线路 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LineServiceImpl implements LineService {

    @Resource
    private LineMapper lineMapper;

    @Override
    public Integer createLine(LineSaveReqVO createReqVO) {
        // 插入
        LineDO line = BeanUtils.toBean(createReqVO, LineDO.class);
        lineMapper.insert(line);

        // 返回
        return line.getId();
    }

    @Override
    public void updateLine(LineSaveReqVO updateReqVO) {
        // 校验存在
        validateLineExists(updateReqVO.getId());
        // 更新
        LineDO updateObj = BeanUtils.toBean(updateReqVO, LineDO.class);
        lineMapper.updateById(updateObj);
    }

    @Override
    public void deleteLine(Integer id) {
        // 校验存在
        validateLineExists(id);
        // 删除
        lineMapper.deleteById(id);
    }

    @Override
        public void deleteLineListByIds(List<Integer> ids) {
        // 删除
        lineMapper.deleteByIds(ids);
        }


    private void validateLineExists(Integer id) {
        if (lineMapper.selectById(id) == null) {
            throw exception(LINE_NOT_EXISTS);
        }
    }

    @Override
    public LineDO getLine(Integer id) {
        return lineMapper.selectById(id);
    }

    @Override
    public PageResult<LineDO> getLinePage(LinePageReqVO pageReqVO) {
        return lineMapper.selectPage(pageReqVO);
    }

}